/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package network;

import network.packets.CallSignChangedResponce;
import network.packets.DriverBalanceChange;
import network.packets.DriverMessageResponce;
import network.packets.DriverParkingPositionResponce;
import network.packets.FilteredTaxiParkingsResponce;
import network.packets.GetCSBalanceResponce;
import network.packets.GetOrdersResponse;
import network.packets.LoginResponce;
import network.packets.OrderResponce;
import network.packets.OrderResponce2;
import network.packets.OrderResponce3;
import network.packets.OrderResponce4;
import network.packets.PPCSettingsResponce;
import network.packets.Packet;
import network.packets.PingResponce;
import network.packets.PreOrdersResponse;
import network.packets.RegisterOnTaxiParkingResponce;
import network.packets.RelayCommunicationResponce;
import network.packets.RequestConfirmationResponce;
import network.packets.SQLResponce;
import network.packets.ServerErrorResponce;
import network.packets.SettingsXML;
import network.packets.SrvMessageResponce;
import network.packets.SrvTransferDataResponce;
import network.packets.TCPMessageResponce;
import network.packets.TaxiParkingStatisticResponce;
import network.packets.TaxiParkingsLastChangeDateResponce;
import network.packets.TaxiParkingsResponce;
import network.packets.TaxiParkingsResponce2;
import network.packets.UnRegisterOnTaxiParkingResponce;
import network.packets.WorkReportResponce;
import utils.StringUtils;
import utils.Utils;

/**
 *
 * @author Kvest
 */
public class PacketParser 
{
    public PacketParser()
    {
        
    }
    
    public Packet parsePacket(byte[] data)
    {
        int id = selectID(data);

        SrvTransferDataResponce tmp = null;
        if (id == Packet.SRV_TRANSFER_DATA_RESPONCE)
        {
            tmp = new SrvTransferDataResponce(data);
            if (tmp.getBody() != null)
            {
                data = tmp.getBody();
                id = selectID(data);
            }
        }
      
        Packet result;
        switch (id)
        {
/*+*/            case Packet.SERVER_ERROR_RESPONCE : result = new ServerErrorResponce(data); break;
/*+*/            case Packet.LOGIN_RESPONCE : result = new LoginResponce(data); break;
/*+*/            case Packet.PING_RESPONCE : result = new PingResponce(data); break; 
/*+*/            case Packet.SRV_MESSAGE_RESPONCE : result = new SrvMessageResponce(data); break; 
/*+*/            case Packet.REGISTER_ON_TAXI_PARKING_RESPONCE: result = new RegisterOnTaxiParkingResponce(data); break;
/*+*/            case Packet.RELAY_COMMUNICATION_RESPONCE: result = new RelayCommunicationResponce(data); break;
/*+*/            case Packet.ORDER_RESPONCE : result = new OrderResponce(data); break;
/*+*/            case Packet.ORDER_RESPONCE2 : result = new OrderResponce2(data); break;
/*+*/            case Packet.ORDER_RESPONCE3 : result = new OrderResponce3(data); break;
                 case Packet.ORDER_RESPONCE4 : result = new OrderResponce4(data); break;
/*+*/            case Packet.TCPMESSAGE_RESPONCE : result = new TCPMessageResponce(data); break;
            case Packet.REQUEST_CONFIRMATION_RESPONCE : result = new RequestConfirmationResponce(data); break;
/*+*/            case Packet.TAXI_PARKING_STATISTIC_RESPONCE : result = new TaxiParkingStatisticResponce(data); break;
/*+*/            case Packet.TAXI_PARKING_LAST_CHANGE_DATE_RESPONCE : result = new TaxiParkingsLastChangeDateResponce(data); break;
            case Packet.WORK_REPORT_RESPONCE : result = new WorkReportResponce(data); break;
/*+*/            case Packet.TAXI_PARKINGS_RESPONCE : result = new TaxiParkingsResponce(data); break;
/*+*/            case Packet.TAXI_PARKINGS_RESPONCE2 : result = new TaxiParkingsResponce2(data); break;    
/*+*/            case Packet.UNREGISTER_ON_TAXI_PARKING_RESPONCE : result = new UnRegisterOnTaxiParkingResponce(data); break;
/*+*/            case Packet.DRIVER_PARKING_POSITION_RESPONCE : result = new DriverParkingPositionResponce(data); break;
/*+*/            case Packet.PPCSETTINGS_RESPONCE : result = new PPCSettingsResponce(data); break;
                 case Packet.SETTINGSXML_RESPONSE : result = new SettingsXML(data); break;
/*+*/            case Packet.CALL_SIGNCHANGED_RESPONCE : result = new CallSignChangedResponce(data); break;
/*+*/            case Packet.GET_ORDERS_RESPONCE : result = new GetOrdersResponse(data); break;
            case Packet.SQL_RESPONCE : result = new SQLResponce(data); break;
/*+*/            case Packet.PREORDERS_RESPONCE : result = new PreOrdersResponse(data); break;
/*+*/            case Packet.CSBALANCE_RESPONCE : result = new GetCSBalanceResponce(data); break;
            case Packet.DRIVER_MESSAGE_RESPONCE : result = new DriverMessageResponce(data); break;
/*-*/            case Packet.DRIVER_BALANCE_CHANGED_RESPONCE : result = new DriverBalanceChange(data); break;  
/*+*/            case Packet.FILTERED_TAXI_PARKINGS_RESPONCE : result = new FilteredTaxiParkingsResponce(data); break; 
            default : result = null; 
        }
        
        //Номер пакета устанавливаем        
        if (tmp != null && result != null)
        {
            result.setPacketNumber(tmp.getPacketNumber());
        }
        
        return result;
    }

    private int selectID(byte[] data)
    {
        //Определяем что за пакет
        byte[] buffer4 = new byte[4];
        int offset = 0;
        System.arraycopy(data, offset, buffer4, 0, buffer4.length);
        offset += buffer4.length;
        int size = Utils.byteToInt(buffer4);
        byte[] buffer = new byte[size];
        System.arraycopy(data, offset, buffer, 0, buffer.length);
        String val = StringUtils.bytesToStr(buffer);
//System.out.println("[" + val + "]");
        if (val.equals("IMAP.Net.SrvTransfereError"))
        {
                return Packet.SERVER_ERROR_RESPONCE;
        }
        else if (val.equals("IMAP.Net.SrvLoginAnswer"))
        {
                return Packet.LOGIN_RESPONCE;
        }
        else if (val.equals("IMAP.Net.SrvPing"))
        {
                return Packet.PING_RESPONCE;
        }
        else if (val.equals("IMAP.Net.SrvMessage"))
        {
                return Packet.SRV_MESSAGE_RESPONCE;
        }
        else if (val.equals("IMAP.Net.SrvTransfereData"))
        {
                return Packet.SRV_TRANSFER_DATA_RESPONCE;
        }
        else if (val.equals("IMAP.Net.RegisterOnTaxiParking_answer"))
        {
                return Packet.REGISTER_ON_TAXI_PARKING_RESPONCE;
        }
        else if (val.equals("IMAP.Net.RelayCommunication"))
        {
                return Packet.RELAY_COMMUNICATION_RESPONCE;
        }
        else if (val.equals("IMAP.Net.DispOrder"))
        {
                return Packet.ORDER_RESPONCE;
        }
        else if (val.equals("IMAP.Net.DispOrder2"))
        {
                return Packet.ORDER_RESPONCE2;
        }
        else if (val.equals("IMAP.Net.DispOrder3"))
        {
                return Packet.ORDER_RESPONCE3;
        }
        else if (val.equals("IMAP.Net.DispOrder4"))
        {
                return Packet.ORDER_RESPONCE4;
        }
        else if (val.equals("IMAP.Net.TCPMessage"))
        {
                return Packet.TCPMESSAGE_RESPONCE;
        }
        else if (val.equals("IMAP.Net.RequestConfirmation"))
        {
                return Packet.REQUEST_CONFIRMATION_RESPONCE;
        }
        else if (val.equals("IMAP.Net.GetTaxiParkingStatistic_answer"))
        {
                return Packet.TAXI_PARKING_STATISTIC_RESPONCE;
        }
        else if (val.equals("IMAP.Net.GetTaxiParkingsLastChangeDate_answer"))
        {
                return Packet.TAXI_PARKING_LAST_CHANGE_DATE_RESPONCE;
        }
        else if (val.equals("IMAP.Net.GetWorkReport_answer"))
        {
                return Packet.WORK_REPORT_RESPONCE;
        }
        else if (val.equals("IMAP.Net.GetTaxiParkings_answer"))
        {
                return Packet.TAXI_PARKINGS_RESPONCE;
        }
        else if (val.equals("IMAP.Net.GetTaxiParkings_answer2"))
        {
                return Packet.TAXI_PARKINGS_RESPONCE2;
        }
        else if (val.equals("IMAP.Net.UnRegisterOnTaxiParking_answer"))
        {
                return Packet.UNREGISTER_ON_TAXI_PARKING_RESPONCE;
        }
        else if (val.equals("IMAP.Net.GetDriverParkingPosition_answer"))
        {
                return Packet.DRIVER_PARKING_POSITION_RESPONCE;
        }
        else if (val.equals("IMAP.Net.PPCSettings"))
        {
                return Packet.PPCSETTINGS_RESPONCE;
        }
        else if (val.equals("IMAP.Net.SettingXml")) 
        {
            return Packet.SETTINGSXML_RESPONSE;
        }
        else if (val.equals("IMAP.Net.CallSignChanged"))
        {
                return Packet.CALL_SIGNCHANGED_RESPONCE;
        }
        else if (val.equals("IMAP.Net.GetOrders_answer") || val.equals("IMAP.Net.InnerNamespace.GetOrders_answer"))
        {
            return Packet.GET_ORDERS_RESPONCE;
        }
        else if (val.equals("IMAP.Net.ExequteQuerryCommand_answer"))
        {
            return Packet.SQL_RESPONCE;
        }
        else if (val.equals("IMAP.Net.DriverBalanceChange"))
        {
            return Packet.DRIVER_BALANCE_CHANGED_RESPONCE;
        }
        else if (val.equals("IMAP.Net.GetPreliminaryOrders_answer"))
        {
            return Packet.PREORDERS_RESPONCE;
        }
        else if (val.equals("IMAP.Net.GetCSBalanceAnswer"))
        {
            return Packet.CSBALANCE_RESPONCE;
        }
        else if (val.equals("IMAP.Net.DriverMessage"))
        {
            return Packet.DRIVER_MESSAGE_RESPONCE;
        }
        else if (val.equals("IMAP.Net.FilteredTaxiPаrkings_answer"))
        {
            return Packet.FILTERED_TAXI_PARKINGS_RESPONCE;
        }
        return Packet.UNKNOWN_RESPONCE;
    }
}
